﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using DALHelper.Business;

namespace ResortManager
{
    /// <summary>
    /// Interaction logic for wndLogin.xaml
    /// </summary>
    public partial class wndLogin : Window
    {
        public wndLogin()
        {
            InitializeComponent();
            LoadInfo();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void expAdvanced_Expanded(object sender, RoutedEventArgs e)
        {
            double i = ((Grid)expAdvanced.Content).Height;
            this.Height += i;
        }

        private void expAdvanced_Collapsed(object sender, RoutedEventArgs e)
        {
            double i = ((Grid)expAdvanced.Content).Height;
            this.Height -= i;
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string sNick = cmbUserName.Text;
            string sPass = txtPass.Password;

            if (string.IsNullOrEmpty(sNick) || string.IsNullOrEmpty(sPass))
            {
                cmbUserName.Focus();
                return;
            }

            try
            {
                SaveInfo();

                BusinessManager busManager;
                if (expAdvanced.IsExpanded)
                {
                    busManager = new BusinessManager(txtServer.Text, txtDBName.Text, txtDBUser.Text, txtDBPass.Password);
                }
                else
                {
                    busManager = new BusinessManager();
                }
                ((App)App.Current).BUSManager = busManager;

               
                if (sNick == "admin" && sPass == "123456")
                {
                    ((App)App.Current).CurrentUserName = sNick;
                    ((App)App.Current).CurrentTransactionID = App.ADMIN_TRANSACTION_ID;

                    wndMain f = new wndMain();
                    f.IsReceptionist = true;
                    f.Show();
                    Close();
                }
                else
                {
                    long lTransID;
                    if (busManager.UserManager.Login(sNick, sPass, out lTransID))
                    {
                        ((App)App.Current).CurrentUserName = sNick;
                        ((App)App.Current).CurrentTransactionID = lTransID;

                        wndMain ff = new wndMain();
                        ff.IsReceptionist = false;
                        ff.Show();
                        Close();
                    }
                    else
                    {
                        MessageBox.Show("Đăng nhập thất bại!", "Failed", MessageBoxButton.OK, MessageBoxImage.Hand);
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Không thể kết nối.\r\nVui lòng kiểm tra lại kết nối mạng và các tham số khác.", "Eror", MessageBoxButton.OK, MessageBoxImage.Hand);
                Debug.WriteLine("Connect error: " + ex.Message);
            }
        }

        private void SaveInfo()
        {
            Util.AddUserList(cmbUserName.Text);
            if (chkRemember.IsChecked == true)
            {
                Util.AddRememberInfo(cmbUserName.Text, txtPass.Password);
            }
            else
            {
                Util.AddRememberInfo(string.Empty, string.Empty);
            }

            if (expAdvanced.IsExpanded)
            {
                Util.AddAdvancedConfig(txtServer.Text, txtDBName.Text, txtDBUser.Text, txtDBPass.Password);
            }
        }

        private void LoadInfo()
        {
            cmbUserName.Items.Clear();
            foreach (string s in Util.GetUserList())
            {
                cmbUserName.Items.Add(s);
            }

            string sUser, sPass;
            if (Util.GetRememberInfo(out sUser, out sPass))
            {
                cmbUserName.Text = sUser;
                txtPass.Password = sPass;
                chkRemember.IsChecked = true;
            }

            string sDBServer, sDBName, sDBAcc, sDBPass;
            if (Util.GetAdvancedConfig(out sDBServer, out sDBName, out sDBAcc, out sDBPass))
            {
                txtServer.Text = sDBServer;
                txtDBName.Text = sDBName;
                txtDBUser.Text = sDBAcc;
                txtDBPass.Password = sDBPass;
            }
        }
    }
}
